package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.custom_widget.AdderSubtractorView;
import com.wangcang.widgetlibrary.keyboard.VirtualKeyboardView;

import java.lang.reflect.Method;

/**
 * 数字加减器Activity
 * Created by ShineF on 2017/6/20 0020.
 */

public class AdderSubtractorActivity extends AppCompatActivity implements AdderSubtractorView.INumberListener, AdderSubtractorView.DNumberListener, View.OnClickListener {

    private VirtualKeyboardView mIntKeyboard; // 数字键盘
    private VirtualKeyboardView mDoubleKeyboard; // 小数键盘
    private EditText mResultInt1;
    private EditText mResultInt2;
    private EditText mResultDouble1;
    private AdderSubtractorView mAdderSubtractorView;
    private LinearLayout mLayout;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adder_subtractor);
        init();
    }

    public void init() {
        mIntKeyboard = (VirtualKeyboardView) findViewById(R.id.int_number);
        mDoubleKeyboard = (VirtualKeyboardView) findViewById(R.id.double_number);
        mLayout = (LinearLayout) findViewById(R.id.ll_layout);
        mAdderSubtractorView = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view);
        AdderSubtractorView mAdderSubtractorView2 = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view_2);
        AdderSubtractorView mAdderSubtractorView3 = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view_3);
        AdderSubtractorView mAdderSubtractorView4 = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view_4);
        AdderSubtractorView mAdderSubtractorView5 = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view_5);
        AdderSubtractorView mAdderSubtractorView6 = (AdderSubtractorView) findViewById(R.id.adder_subtractor_view_6);

        mAdderSubtractorView.setStyle(1);//设置选择器的样式
        mAdderSubtractorView.setLeastValue(10);//设置最小值
        mAdderSubtractorView.setMaximumValue(99);//设置最大值
        mAdderSubtractorView.setDefaultValue(10);//设置默认值
        mAdderSubtractorView.setIntNumberListener(this);//添加当数字到达两界点时的监听
        mResultInt1 = mAdderSubtractorView.getEditText();

        mAdderSubtractorView2.setStyle(1);//设置选择器的样式
        mAdderSubtractorView2.setLeastValue(-10);//设置最小值
        mAdderSubtractorView2.setMaximumValue(99);//设置最大值
        mAdderSubtractorView2.setDefaultValue(-5);//设置默认值
        mAdderSubtractorView2.setWhetherToEnter(false);
        mAdderSubtractorView2.setIntNumberListener(this);//添加当数字到达两界点时的监听

        mAdderSubtractorView3.setStyle(2);//设置选择器的样式
        mAdderSubtractorView3.setLeastValue(0);//设置最小值
        mAdderSubtractorView3.setMaximumValue(99);//设置最大值
        mAdderSubtractorView3.setDefaultValue(0);//设置默认值
        mAdderSubtractorView3.setIntNumberListener(this);//添加当数字到达两界点时的监听
        mResultInt2 = mAdderSubtractorView3.getEditText();

        mAdderSubtractorView4.setStyle(2);//设置选择器的样式
        mAdderSubtractorView4.setLeastValue(0);//设置最小值
        mAdderSubtractorView4.setMaximumValue(99);//设置最大值
        mAdderSubtractorView4.setDefaultValue(0);//设置默认值
        mAdderSubtractorView4.setWhetherToEnter(false);
        mAdderSubtractorView4.setIntNumberListener(this);//添加当数字到达两界点时的监听

        mAdderSubtractorView5.setStyle(1);//设置选择器的样式
        mAdderSubtractorView5.setDataType(AdderSubtractorView.DOUBLE);//设置是否是小数
        mAdderSubtractorView5.setDLeastValue(0.0);//设置最小值
        mAdderSubtractorView5.setDMaximumValue(99.99);//设置最大值
        mAdderSubtractorView5.setDDefaultValue(0.0);//设置默认值
        mAdderSubtractorView5.setWhetherToEnter(true);
        mAdderSubtractorView5.setDoubleNumberListener(this);//添加当数字到达两界点时的监听
        mResultDouble1 = mAdderSubtractorView5.getEditText();

        mAdderSubtractorView6.setStyle(2);//设置选择器的样式
        mAdderSubtractorView6.setDataType(AdderSubtractorView.DOUBLE);//设置是否是小数
        mAdderSubtractorView6.setDLeastValue(0.0);//设置最小值
        mAdderSubtractorView6.setDMaximumValue(99.99);//设置最大值
        mAdderSubtractorView6.setDDefaultValue(0.0);//设置默认值
        mAdderSubtractorView6.setWhetherToEnter(false);
        mAdderSubtractorView6.setDoubleNumberListener(this);//添加当数字到达两界点时的监听

        hideSystemKeyboard(mResultInt1, mResultInt2, mResultDouble1);
        mLayout.setOnClickListener(this);
        mResultInt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showKeyboardInt(mResultInt1);
            }
        });
        mResultInt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showKeyboardInt(mResultInt2);
            }
        });
        mResultDouble1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showKeyboardDouble();
            }
        });
        mResultInt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                showKeyboardInt(mResultInt1);
            }
        });
        mResultInt2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                showKeyboardInt(mResultInt2);
            }
        });
        mResultDouble1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                showKeyboardDouble();
            }
        });

    }

    public void showKeyboardInt(EditText editText) {
        mIntKeyboard.setFocusable(true);
        mIntKeyboard.setFocusableInTouchMode(true);
        mIntKeyboard.setEditText(editText);
        if (mIntKeyboard.getVisibility() != View.VISIBLE) {
            mIntKeyboard.setVisibility(View.VISIBLE);
            mDoubleKeyboard.setVisibility(View.GONE);
        }
    }

    public void showKeyboardDouble() {
        mDoubleKeyboard.setFocusable(true);
        mDoubleKeyboard.setFocusableInTouchMode(true);
        mDoubleKeyboard.setEditText(mResultDouble1);
        if (mDoubleKeyboard.getVisibility() != View.VISIBLE) {
            mDoubleKeyboard.setVisibility(View.VISIBLE);
            mIntKeyboard.setVisibility(View.GONE);
        }
    }

    @Override
    public void setMoreListener(int number, View view) {
        Toast.makeText(this, "最大", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setLeastListener(int number, View view) {
        Toast.makeText(this, "最小", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setMoreListener(double number, View view) {
        Toast.makeText(this, "最大", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setLeastListener(double number, View view) {
        Toast.makeText(this, "最小", Toast.LENGTH_SHORT).show();
    }

    private void hideSystemKeyboard(EditText... mNumber) {
        for (int i = 0; i < mNumber.length; i++) {
            // 设置不调用系统键盘
            if (android.os.Build.VERSION.SDK_INT <= 10) {
                mNumber[i].setInputType(InputType.TYPE_NULL);
            } else {
                this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

                Class<EditText> clazz = EditText.class;
                Method method;

                try {
                    method = clazz.getMethod("setShowSoftInputOnFocus", boolean.class);
                    method.setAccessible(true);
                    method.invoke(mNumber[i], false);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    method = clazz.getMethod("setSoftInputShownOnFocus", boolean.class);
                    method.setAccessible(true);
                    method.invoke(mNumber[i], false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_layout:
                if (mIntKeyboard.getVisibility() == View.VISIBLE) {
                    mIntKeyboard.setVisibility(View.GONE);
                }
                if (mDoubleKeyboard.getVisibility() == View.VISIBLE) {
                    mDoubleKeyboard.setVisibility(View.GONE);
                }
                break;
        }
    }
}
